﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Web.SessionState;

namespace SES.CMS.Module
{
    public partial class ucCookie : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load();
        }

        public void load()
        {
            if (Request.Cookies["DaXem"] != null | Convert.ToString(Request.Cookies["DaXem"]) != "")
            {
                HttpCookie ckaccount = Request.Cookies["DaXem"];
                string s = ckaccount["IDSP"].ToString();
                string[] separator = new string[] { "-" };

                string[] kq = s.Split(separator, StringSplitOptions.RemoveEmptyEntries);
                DataTable data = new DataTable();
                if (kq.Length >= 6)
                {
                    for (int i = kq.Length; i > kq.Length - 6; i--)
                    {
                        DataTable dt = new cmsSanPhamBL().SelectByID(new cmsSanPhamDO { SanPhamID = int.Parse(kq[i - 1]) });
                        data.Merge(dt);
                    }
                }
                else
                {
                    for (int i = kq.Length; i > 0; i--)
                    {
                        DataTable dt = new cmsSanPhamBL().SelectByID(new cmsSanPhamDO { SanPhamID = int.Parse(kq[i - 1]) });
                        data.Merge(dt);
                    }
                }
                rpt.DataSource = data;
                rpt.DataBind();
            }
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public cmsGioHangDO init()
        {
            cmsGioHangDO obj = new cmsGioHangDO();
            if (Session["ClientLoginID"] == null)
                obj.ClientLoginID = 0;
            else
                obj.ClientLoginID = int.Parse(Session["ClientLoginID"].ToString());
            obj.NgayDatHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.NgayGiaoHang = Convert.ToDateTime(DateTime.Now.ToString());
            obj.SoLuong = 1;
            obj.XuLy = false;
            return obj;
        }
        protected void rpt_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            try
            {
                HttpSessionState ss = HttpContext.Current.Session;
                string session = ss.SessionID;
                Int64 id = Int64.Parse(e.CommandArgument.ToString());
                cmsGioHangDO obj = new cmsGioHangBL().SelecBySessionAndSP(session, id);
                cmsSanPhamDO obj1 = new cmsSanPhamBL().Select(new cmsSanPhamDO { SanPhamID = id });
                float gia = (float)(obj1.DonGia - (obj1.DonGia * obj1.GiamGia) / 100);

                if (obj.GioHangID < 1)
                {
                    obj = init();
                    obj.SessionID = session;
                    obj.SanPhamID = id;
                    obj.ThanhTien = gia * obj.SoLuong;
                    new cmsGioHangBL().Insert(obj);
                    Ultility.Alert("Đặt mua thành công!", "http://" + Request.Url.Host + Request.Url.AbsolutePath);
                }

                else
                {
                    obj.SoLuong++;
                    obj.ThanhTien += gia;
                    new cmsGioHangBL().Update(obj);
                    Ultility.Alert("Đặt mua thành công!", "http://" + Request.Url.Host + Request.Url.AbsolutePath);
                }
            }
            catch
            {
                Ultility.Alert("Có lỗi xảy ra!", "http://" + Request.Url.Host + Request.Url.AbsolutePath);
            }
        }
    }
}